﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Cinser.Common;
using System.Windows.Threading;

namespace Cinser.Test.WinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.FormClosed += Form1_FormClosed;
        }

        void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
            this.Dispose();
            this.Close();
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            DBUtility.DbHelperOra.connectionString = Common.ConfigurationHelper.GetAppSettingsValue("ConnectionString");
            DataSet ds = DBUtility.DbHelperOra.Query("select * from message");
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        /// <summary>
        /// XML转Oracle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string msg = string.Empty;
            int iCount = 0;
            int iAddCount = 0;
            string path = textBox1.Text;
            List<string>xmlFiles = Cinser.Common.FileHelper.GetFileNamesFromADirectory(path, "xml");
            foreach (var item in xmlFiles)
            {
                Cinser.Common.XmlHelper xml = new Common.XmlHelper(item);
                string tbName = xml.FirstNode.Name;
                List<XmlNode> nodes = xml.FirstNode.FindNode("item",false);
                if(nodes!=null && nodes.Count>0)
                {
                    foreach (XmlNode node in nodes)
                    {
                        Dictionary<string, string> attrs = node.GetAttributes();
                        if (attrs.Count > 0)
                        {
                            if (DBUtility.OracleHelper.HasTable(ConnStr, tbName) == false)
                            {
                                string sql = "";
                                sql += string.Format("create table {0}(", tbName);
                                foreach (string key in attrs.Keys.ToList())
                                {
                                    int lenght = 50;
                                    if (attrs[key].Length * 3 > lenght)
                                        lenght = attrs[key].Length * 3;
                                    sql += string.Format(" {0} NVARCHAR2({1}),", key, lenght);
                                }

                                sql = StringPlus.DelLastChar(sql, ",");
                                sql += " )";
                                try
                                {
                                    DBUtility.OracleHelper.ExecuteNonQuery(ConnStr, sql);
                                }
                                catch (Exception ex)
                                {
                                    msg += string.Format("{0}.执行失败：{1} \n" ,sql, ex.Message);
                                }
                                iCount++;
                            }
                            else
                            {
                                string sql = "";
                                string columnsStr = StringPlus.GetArrayStr(attrs.Keys.ToList(), ",");
                                string valueStr = string.Empty;
                                foreach (string key in attrs.Keys.ToList())
                                {
                                    valueStr += string.Format("'{0}',", attrs[key]);
                                }

                                valueStr = StringPlus.DelLastChar(valueStr, ",");
                                sql = string.Format("insert into {0} ({1}) values ({2})", tbName, columnsStr, valueStr);
                                try
                                {
                                    DBUtility.OracleHelper.ExecuteNonQuery(ConnStr, sql);
                                }
                                catch (Exception ex)
                                {
                                    msg += string.Format("{0}.执行失败：{1} \n", sql, ex.Message);
                                }
                                iAddCount++;
                            }
                        }
                    }
                }
            }
            MessageBox.Show("创建了" + iCount + "张表格！添加了" + iAddCount + "条数据。" + msg);
        }

        public string ConnStr
        {
            get
            {
                return Cinser.Common.ConfigurationHelper.GetAppSettingsValue("ConnectionString");
            }
        }

        Cinser.Common.Wpf.Controls.WinProgressPlus win;
        private void button1_Click(object sender, EventArgs e)
        {
            win = new Cinser.Common.Wpf.Controls.WinProgressPlus("测试", new Cinser.Common.Wpf.Controls.NoReturnHandler(DoWork));
            win.Show();
        }
        public void DoWork()
        {
            while (win.CurrentPercent <= 100)
            {
                win.ContentList.Append(new Cinser.Common.Wpf.Controls.ProgressListItem("List:" + DateTime.Now.ToString(), Cinser.Common.Wpf.Controls.ProgImageTypeEm.Succeed));
                win.CurrentPercent++;
                win.CurrentOperation = "当前操作：" + DateTime.Now.ToString();
                win.Subtitle = "小标题" + DateTime.Now.ToString();
                System.Threading.Thread.Sleep(150);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate
            {
                int icount = 0;
                while (true)
                {
                    this.Invoke(new Action(() =>
                    {
                        label1.Text = DateTime.Now.ToString();
                        this.progressBar1.Value = icount++;                       
                    }));
                    if (icount >= 100)
                        icount = 1;
                    System.Threading.Thread.Sleep(200);
                }
            }));
            thread.IsBackground = true;
            thread.Start();
        }
    }
}
